package com.itcast.stargraph.handler;

import cn.hutool.json.JSONUtil;
import com.itcast.stargraph.pojo.MessageBase;
import com.itcast.stargraph.service.ComfyuiMsgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

/**
 * TODO
 *
 * @Author katana
 * @Date 2025/6/8 16:33
 */
@Component
@Slf4j
public class ComfyuiMessageHandler extends TextWebSocketHandler {
    @Autowired
    private ComfyuiMsgService comfyuiMsgService;
    
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        log.info("星图AI应用程序Client与ComfyUI服务端连接成功");
    }
    
    
    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        log.info("收到消息：{}", message.getPayload());
        comfyuiMsgService.handleMsg(JSONUtil.toBean(message.getPayload(), MessageBase.class));
    }
}
